package com.qfedu.yige.service.imp;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qfedu.yige.mapper.UserMapper;
import com.qfedu.yige.pojo.PowerUser;
import com.qfedu.yige.service.UserService;
import com.qfedu.yige.service.UserService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceimp extends ServiceImpl<UserMapper, PowerUser> implements UserService, UserDetailsService {

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<PowerUser> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("username",username);
        PowerUser tbUser = this.baseMapper.selectOne(queryWrapper);
        if (ObjUtil.isNotEmpty(tbUser)){

            List<GrantedAuthority> authorities =new ArrayList<>();//正常应该放该用户拥有的角色的集合

            return new User(username,tbUser.getPassword(),authorities);

        }
        System.out.println("用户名不存在");
        return null;
    }

}
